<template>
  <div class="app-container">
    <el-form
      @submit.native.prevent
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item :label="`${$courseName?$courseName:'课程'}名称`" prop="postcourseNameCode">
        <el-input
          v-input-restrict
          v-model="queryParams.courseName"
          :placeholder="`请输入${$courseName?$courseName:'课程'}名称`"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button v-preventReClick
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button v-preventReClick icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-preventReClick
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="multiple"
          @click="handleUpdateSome"
          >修改</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-table
      v-loading="loading"
      :data="postList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column :label="`${$courseName?$courseName:'课程'}`" align="center" prop="courseName"/>
      <el-table-column :label="`${$hourAmount?$hourAmount:'课时量'}(时)`" align="center" prop="studyTime"/>
      <el-table-column label="补考次数" align="center" prop="applyExamTimes" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form @submit.native.prevent ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item
          v-if="title == '修改补考次数'"
          :label="`${$courseName?$courseName:'课程'}名称`"
          prop="courseName"
        >
          {{ form.courseName }}
        </el-form-item>
        <el-form-item v-if="title == '修改补考次数'" :label="`${$hourAmount?$hourAmount:'课时量'}`" prop="day">
          {{ form.studyTime }}
        </el-form-item>
        <el-form-item label="补考次数" prop="applyLimit">
          <el-input
          v-input-restrict-number
            :step="1"
            style="width: 180px!important;"
            :precision="0"
            v-model="form.applyLimit"
            controls-position="right"
            placeholder="请输入补考次数"
            :min="0"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button v-preventReClick type="primary" @click="submitForm">确 定</el-button>

      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  updatePost_courseMajor,
  getPost_courseMajor,
  updatePost_courseMajorSum,
  updateCourseExamTimes
} from "@/api/configuration/makeupConfiguration";
import {
  listPost_course,
} from "@/api/course/course";

export default {
  name: "Post",
  dicts: ["sys_course_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        status:0,
        courseName: '',
      },
      // 表单参数
      form: {
        applyLimit: undefined,
        id: undefined,
      },
      // 表单校验
      rules: {
        applyLimit: [
          { required: true, message: "补考次数不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      this.queryParams.schoolId = this.$store.getters.schoolId;
      listPost_course(this.queryParams).then((response) => {
        this.postList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.form = {
        applyLimit: undefined,
        id: undefined,
      };
      this.open = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$refs['queryForm'].resetFields();
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.courseId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.form = row
      this.open = true;
      this.title = "修改补考次数";
    },
    handleUpdateSome() {
      this.form={}
      this.open = true;
      this.title = "批量修改补考次数";
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.title == "修改补考次数") {
            updateCourseExamTimes(this.form.applyLimit,this.form.courseId).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            updateCourseExamTimes(this.form.applyLimit, this.ids.join(',')).then((res) => {
              this.$modal.msgSuccess("批量修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep .el-menu-item:hover{
  border-bottom: 1px solid #e6e6e6!important;
}
::v-deep .el-menu--horizontal>.el-menu-item.is-active:hover{
  border-bottom: 2px solid #11A983!important;
}
</style>
